﻿#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

#define PI 3.1415926

struct Round
{
	float x;
	float y;
	float radius;
	int angle_num;

};

Round rounds[5][5];
int step = 10;
int score = 0;

void rotate_round(int i, int j)
{
	rounds[i][j].angle_num -= 1;
	if (rounds[i][j].angle_num < 0)
	{
		rounds[i][j].angle_num = 3;
	}
	score += 90;
}


int next_indexes(int indexes[2])
{
	int i = indexes[0];
	int j = indexes[1];

	if (rounds[i][j].angle_num == 0)
	{
		j++;
	}
	else if (rounds[i][j].angle_num == 3)
	{
		i++;
	}
	else if (rounds[i][j].angle_num == 2)
	{
		j--;
	}
	else if (rounds[i][j].angle_num == 1)
	{
		i--;
	}

	indexes[0] = i;
	indexes[1] = j;

	if (i >= 0 && i < 5 && j >= 0 && j < 5)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


void  startup()
{
	initgraph(600, 700);
	setbkcolor(RGB(50, 50, 50));
	setlinestyle(PS_SOLID, 3);
	cleardevice();

	BeginBatchDraw();

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			rounds[i][j].x = 100 + j * 100;
			rounds[i][j].y = 200 + i * 100;
			rounds[i][j].radius = 30;
			rounds[i][j].angle_num = 1;

		}
	}
}


void show()
{
	float angle;
	cleardevice();

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			setlinecolor(RGB(200, 200, 200));
			circle(rounds[i][j].x, rounds[i][j].y, rounds[i][j].radius);
			setlinecolor(RED);
			angle = rounds[i][j].angle_num * PI / 2;
			line(rounds[i][j].x, rounds[i][j].y, rounds[i][j].x + rounds[i][j].radius * cos(-angle), rounds[i][j].y + rounds[i][j].radius * sin(-angle));
		}
	}

	TCHAR text[20];
	setbkmode(TRANSPARENT);
	swprintf_s(text, _T("%d 步 %d 度"), step, score);
	settextstyle(50, 0, _T("楷体"));
	outtextxy(150, 30, text);
	settextstyle(20, 0, _T("楷体"));
	outtextxy(15, 100, _T("点击一个圆圈，其指针顺时针旋转90度后，指向的指针依次旋转"));

	FlushBatchDraw();

	FlushBatchDraw();
}


void update()
{
	MOUSEMSG  mouse;
	if (MouseHit())
	{
		mouse = GetMouseMsg();
		if (mouse.uMsg == WM_LBUTTONDOWN && step > 0)
		{
			step--;
			int clicked_i = int(mouse.y - 150) / 100;
			int clicked_j = int(mouse.x - 50) / 100;
			rotate_round(clicked_i, clicked_j);
			show();
			Sleep(300);

			int indexes[2] = { clicked_i, clicked_j };

			while (next_indexes(indexes))
			{
				rotate_round(indexes[0], indexes[1]);
				show();
				Sleep(300);
			}
		}
	}
}


int main()
{
	startup();
	while (1)
	{
		show();
		update();

		if (step == 0)
		{
			if (_kbhit())
			{
				int input = _getch();
				if (input == ' ')
				{
					break;
				}
			}
		}
	}
	return 0;
}